package com.fingard.dsp.bank.directbank.cmb03;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;

/**
 * 招行北京分行代付系统单笔代付
 * 
 * @author Cruise
 * @date 2019/6/26
 */
public class CMB03SinglePay extends CMB03Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1908);
		
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "");
			if (actItem != null) {
				JSONObject tmpJsonRequest = new JSONObject();
				
				JSONObject tmpReqHeader = new JSONObject();
				tmpReqHeader.put("CODE", CMB03TransCode.singlePay_1908_190801);
				tmpReqHeader.put("BKDT", "");
				tmpReqHeader.put("BKSER", "");
				tmpReqHeader.put("CMDT", Format.DateTime14Format.format(new Date()));
				tmpReqHeader.put("CMSER", p_req1908.singleDetail.askNumber);
				tmpReqHeader.put("PRDNO", actItem.merchantId);
				tmpReqHeader.put("PARTER", actItem.inMerchantId);
				tmpReqHeader.put("PDTNO", actItem.goodNo);
				
				tmpJsonRequest.put("HEAD", tmpReqHeader);
				
				JSONObject tmpDetailParams = new JSONObject();
				tmpDetailParams.put("YURREF", p_req1908.singleDetail.askNumber);
				tmpDetailParams.put("DEACCTNO", p_req1908.singleDetail.payAct);
				tmpDetailParams.put("CRACCTNO", p_req1908.singleDetail.recAct);
				tmpDetailParams.put("CRACCTNAME", p_req1908.singleDetail.recName);
				tmpDetailParams.put("CRBANKNAME", p_req1908.singleDetail.cnapsName);
				tmpDetailParams.put("CRCHGNO", p_req1908.singleDetail.cnapsCode);
				tmpDetailParams.put("CRBANKADR", p_req1908.singleDetail.recBankLocation);
				tmpDetailParams.put("TRXTXT", p_req1908.singleDetail.useType + "+" + p_req1908.singleDetail.askNumber);
				tmpDetailParams.put("TRNAMT", p_req1908.singleDetail.payAmount);
				if ("1".equals(p_req1908.singleDetail.isPrivate)) {
					tmpDetailParams.put("CRACCTCLASS", "2");
				} else {
					tmpDetailParams.put("CRACCTCLASS", "1");
				}
				
				JSONArray jsonDetailArray = new JSONArray();
				jsonDetailArray.add(tmpDetailParams);
				JSONObject jsonArray = new JSONObject();
				jsonArray.put("SINGPAYX", jsonDetailArray);
				tmpJsonRequest.put("BODY", jsonArray);
				
				JSONObject tmpRootJsonReq = new JSONObject();
				tmpRootJsonReq.put("ROOT", tmpJsonRequest);

				String tmpStrRet = sendToBank(tmpRootJsonReq, actItem);
				
				tmpRetDetail.respCode = "0";
				if (StringHelper.hasAnyChar(tmpStrRet)) {
					tmpStrRet = tmpStrRet.substring(0, tmpStrRet.indexOf("&signTime"));
					JSONObject respBizContent = JSON.parseObject(tmpStrRet);

					JSONObject respBodyJson = respBizContent.getJSONObject("ROOT").getJSONObject("BODY");
					String tmpRespCode = respBodyJson.getString("TRNRESP");
					if ("CMBMB99".equals(tmpRespCode)) {
						JSONArray jsonDetail = respBodyJson.getJSONArray("SINGPAYZ");
						if(jsonDetail.size() > 0){
							for(int i = 0; i < jsonDetail.size(); i++){
		                        JSONObject detailJson = jsonDetail.getJSONObject(i);
		                        tmpRetDetail.respInfo = detailJson.getString("RTNMSG");
		        				tmpRetDetail.transState = detailJson.getString("RTNCOD");
		        				tmpRetDetail.payInfoCode = detailJson.getString("RTNCOD");
							}
						} 
					} else {
						tmpRetDetail.respInfo = respBodyJson.getString("TRNMSG");
						tmpRetDetail.transState = tmpRespCode;
						tmpRetDetail.payInfoCode = tmpRespCode;
					}
				} else {
					tmpRetDetail.respInfo = "银行响应内容为空,请联系银行咨询!";
					tmpRetDetail.transState = PaymentState.FAIL_Payment;
					tmpRetDetail.payInfoCode = FailCode.E9981;
				}
			} else {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = "未找到账号:[" + p_req1908.singleDetail.payAct + "]商户配置信息,请检查config.xml中'actSet'节点相关配置信息!";
				tmpRetDetail.transState = PaymentState.FAIL_Payment;
				tmpRetDetail.payInfoCode = FailCode.E9981;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1944);
		
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1944.payAct, "");
			if (actItem != null) {
				JSONObject tmpJsonRequest = new JSONObject();
				
				JSONObject tmpReqHeader = new JSONObject();
				tmpReqHeader.put("CODE", CMB03TransCode.singlePay_1944_190801);
				tmpReqHeader.put("BKDT", "");
				tmpReqHeader.put("BKSER", "");
				tmpReqHeader.put("CMDT", Format.DateTime14Format.format(new Date()));
				tmpReqHeader.put("CMSER", p_req1944.askNumber);
				tmpReqHeader.put("PRDNO", actItem.merchantId);
				tmpReqHeader.put("PARTER", actItem.inMerchantId);
				tmpReqHeader.put("PDTNO", actItem.goodNo);
				
				tmpJsonRequest.put("HEAD", tmpReqHeader);
				
				JSONObject tmpDetailParams = new JSONObject();
				tmpDetailParams.put("YURREF", p_req1944.askNumber);
				
				JSONArray jsonDetailArray = new JSONArray();
				jsonDetailArray.add(tmpDetailParams);
				JSONObject jsonArray = new JSONObject();
				jsonArray.put("SINGQRYX", jsonDetailArray);
				tmpJsonRequest.put("BODY", jsonArray);
				
				JSONObject tmpRootJsonReq = new JSONObject();
				tmpRootJsonReq.put("ROOT", tmpJsonRequest);
				
				String tmpStrRet = sendToBank(tmpRootJsonReq, actItem);
				
				tmpRetDetail.respCode = "0";
				if (StringHelper.hasAnyChar(tmpStrRet)) {
					tmpStrRet = tmpStrRet.substring(0, tmpStrRet.indexOf("&signTime"));
					JSONObject respBizContent = JSON.parseObject(tmpStrRet);

					JSONObject respBodyJson = respBizContent.getJSONObject("ROOT").getJSONObject("BODY");
					String tmpRespCode = respBodyJson.getString("TRNRESP");
					if ("CMBMB99".equals(tmpRespCode)) {
						JSONArray jsonDetail = respBodyJson.getJSONArray("SINGQRYZ");
						if(jsonDetail.size() > 0) {
							for(int i = 0; i < jsonDetail.size(); i++) {
		                        JSONObject detailJson = jsonDetail.getJSONObject(i);
		                        
		                        String retMsg = detailJson.getString("RTNMSG");
		                        tmpRetDetail.respInfo = retMsg;
		                        String resultMsg = detailJson.getString("RESULT").trim();
		                        if (StringHelper.hasAnyChar(resultMsg)) {
		                        	tmpRetDetail.respInfo = resultMsg;
		                        }
		        				tmpRetDetail.transState = detailJson.getString("STATUS");
		        				tmpRetDetail.payInfoCode = detailJson.getString("RTNCOD");
		        				tmpRetDetail.transAmt = detailJson.getString("TRNAMT");
							}
						} 
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = respBodyJson.getString("TRNMSG");
					}
				} else {
					tmpRetDetail.respInfo = "银行响应内容为空,请联系银行咨询!";
					tmpRetDetail.transState = PaymentState.FAIL_Payment;
					tmpRetDetail.payInfoCode = FailCode.E9981;
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "未找到账号:[" + p_req1944.payAct	+ "]商户配置信息,请检查config.xml中'actSet'节点相关配置信息!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT)	throws Exception {
		return null;
	}
}